home *** CD-ROM | disk | FTP | other *** search
/ Chip 2007 January, February, March & April / Chip-Cover-CD-2007-02.iso / Pakiet bezpieczenstwa / mini Pentoo LiveCD 2006.1 / mpentoo-2006.1.iso / livecd.squashfs / usr / share / gtk-2.0 / demo / sizegroup.c < prev    next >
Encoding:
C/C++ Source or Header  |  2006-04-25  |  5.1 KB  |  168 lines

  1. /* Size Groups
  2.  *
  3.  * GtkSizeGroup provides a mechanism for grouping a number of
  4.  * widgets together so they all request the same amount of space.
  5.  * This is typically useful when you want a column of widgets to 
  6.  * have the same size, but you can't use a GtkTable widget.
  7.  * 
  8.  * Note that size groups only affect the amount of space requested,
  9.  * not the size that the widgets finally receive. If you want the
  10.  * widgets in a GtkSizeGroup to actually be the same size, you need
  11.  * to pack them in such a way that they get the size they request
  12.  * and not more. For example, if you are packing your widgets
  13.  * into a table, you would not include the GTK_FILL flag.
  14.  */
  15.  
  16. #include <gtk/gtk.h>
  17.  
  18. static GtkWidget *window = NULL;
  19.  
  20. /* Convenience function to create a combo box holding a number of strings
  21.  */
  22. GtkWidget *
  23. create_combo_box (const char **strings)
  24. {
  25.   GtkWidget *combo_box;
  26.   const char **str;
  27.  
  28.   combo_box = gtk_combo_box_new_text ();
  29.   
  30.   for (str = strings; *str; str++)
  31.     gtk_combo_box_append_text (GTK_COMBO_BOX (combo_box), *str);
  32.  
  33.   gtk_combo_box_set_active (GTK_COMBO_BOX (combo_box), 0);
  34.  
  35.   return combo_box;
  36. }
  37.  
  38. static void
  39. add_row (GtkTable     *table,
  40.      int           row,
  41.      GtkSizeGroup *size_group,
  42.      const char   *label_text,
  43.      const char  **options)
  44. {
  45.   GtkWidget *combo_box;
  46.   GtkWidget *label;
  47.  
  48.   label = gtk_label_new_with_mnemonic (label_text);
  49.   gtk_misc_set_alignment (GTK_MISC (label), 0, 1);
  50.   gtk_table_attach (GTK_TABLE (table), label,
  51.             0, 1,                  row, row + 1,
  52.             GTK_EXPAND | GTK_FILL, 0,
  53.             0,                     0);
  54.   
  55.   combo_box = create_combo_box (options);
  56.   gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo_box);
  57.   gtk_size_group_add_widget (size_group, combo_box);
  58.   gtk_table_attach (GTK_TABLE (table), combo_box,
  59.             1, 2,                  row, row + 1,
  60.             0,                     0,
  61.             0,                     0);
  62. }
  63.  
  64. static void
  65. toggle_grouping (GtkToggleButton *check_button,
  66.          GtkSizeGroup    *size_group)
  67. {
  68.   GtkSizeGroupMode new_mode;
  69.  
  70.   /* GTK_SIZE_GROUP_NONE is not generally useful, but is useful
  71.    * here to show the effect of GTK_SIZE_GROUP_HORIZONTAL by
  72.    * contrast.
  73.    */
  74.   if (gtk_toggle_button_get_active (check_button))
  75.     new_mode = GTK_SIZE_GROUP_HORIZONTAL;
  76.   else
  77.     new_mode = GTK_SIZE_GROUP_NONE;
  78.   
  79.   gtk_size_group_set_mode (size_group, new_mode);
  80. }
  81.  
  82. GtkWidget *
  83. do_sizegroup (GtkWidget *do_widget)
  84. {
  85.   GtkWidget *table;
  86.   GtkWidget *frame;
  87.   GtkWidget *vbox;
  88.   GtkWidget *check_button;
  89.   GtkSizeGroup *size_group;
  90.  
  91.   static const char *color_options[] = {
  92.     "Red", "Green", "Blue", NULL
  93.   };
  94.   
  95.   static const char *dash_options[] = {
  96.     "Solid", "Dashed", "Dotted", NULL
  97.   };
  98.   
  99.   static const char *end_options[] = {
  100.     "Square", "Round", "Arrow", NULL
  101.   };
  102.   
  103.   if (!window)
  104.     {
  105.       window = gtk_dialog_new_with_buttons ("GtkSizeGroup",
  106.                         GTK_WINDOW (do_widget),
  107.                         0,
  108.                         GTK_STOCK_CLOSE,
  109.                         GTK_RESPONSE_NONE,
  110.                         NULL);
  111.       gtk_window_set_resizable (GTK_WINDOW (window), FALSE);
  112.       
  113.       g_signal_connect (window, "response",
  114.             G_CALLBACK (gtk_widget_destroy), NULL);
  115.       g_signal_connect (window, "destroy",
  116.             G_CALLBACK (gtk_widget_destroyed), &window);
  117.  
  118.       vbox = gtk_vbox_new (FALSE, 5);
  119.       gtk_box_pack_start (GTK_BOX (GTK_DIALOG (window)->vbox), vbox, TRUE, TRUE, 0);
  120.       gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
  121.  
  122.       size_group = gtk_size_group_new (GTK_SIZE_GROUP_HORIZONTAL);
  123.       
  124.       /* Create one frame holding color options
  125.        */
  126.       frame = gtk_frame_new ("Color Options");
  127.       gtk_box_pack_start (GTK_BOX (vbox), frame, TRUE, TRUE, 0);
  128.  
  129.       table = gtk_table_new (2, 2, FALSE);
  130.       gtk_container_set_border_width (GTK_CONTAINER (table), 5);
  131.       gtk_table_set_row_spacings (GTK_TABLE (table), 5);
  132.       gtk_table_set_col_spacings (GTK_TABLE (table), 10);
  133.       gtk_container_add (GTK_CONTAINER (frame), table);
  134.  
  135.       add_row (GTK_TABLE (table), 0, size_group, "_Foreground", color_options);
  136.       add_row (GTK_TABLE (table), 1, size_group, "_Background", color_options);
  137.  
  138.       /* And another frame holding line style options
  139.        */
  140.       frame = gtk_frame_new ("Line Options");
  141.       gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  142.  
  143.       table = gtk_table_new (2, 2, FALSE);
  144.       gtk_container_set_border_width (GTK_CONTAINER (table), 5);
  145.       gtk_table_set_row_spacings (GTK_TABLE (table), 5);
  146.       gtk_table_set_col_spacings (GTK_TABLE (table), 10);
  147.       gtk_container_add (GTK_CONTAINER (frame), table);
  148.  
  149.       add_row (GTK_TABLE (table), 0, size_group, "_Dashing", dash_options);
  150.       add_row (GTK_TABLE (table), 1, size_group, "_Line ends", end_options);
  151.  
  152.       /* And a check button to turn grouping on and off */
  153.       check_button = gtk_check_button_new_with_mnemonic ("_Enable grouping");
  154.       gtk_box_pack_start (GTK_BOX (vbox), check_button, FALSE, FALSE, 0);
  155.       
  156.       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (check_button), TRUE);
  157.       g_signal_connect (check_button, "toggled",
  158.             G_CALLBACK (toggle_grouping), size_group);
  159.     }
  160.  
  161.   if (!GTK_WIDGET_VISIBLE (window))
  162.     gtk_widget_show_all (window);
  163.   else
  164.     gtk_widget_destroy (window);
  165.  
  166.   return window;
  167. }
  168.